<?php
//
  $mysql_connect = mysql_connect( DB_CORE_ADDRESS, DB_CORE_USERNAME, DB_CORE_PASSWORD );
  $mysql_charset = mysql_query("SET NAMES utf8");
  $mysql_select_db = mysql_select_db( DB_CORE_DATABASE );
//
//Odpowiedzialność za tok walki
  function monsterFight( $monsterHealth, $playerHealth, $monsterAttack, $playerAttack, $monsterDefense, $playerDefense ) {
      $numberAttack = 0;
      do{
        if( $numberAttack % 2 == 0 ) {
          $randMonster = rand( 1,10 );
          $monsterHit = round( ( $monsterAttack / $playerDefense ) * 10 / $randMonster );
          $playerHealth = $playerHealth - $monsterHit;
        } else {
          $randPlayer = rand( 1,10 );
          $playerHit = round( ( $playerAttack / $monsterDefense ) * 10 / $randPlayer );
          $monsterHealth = $monsterHealth - $playerHit;
        }
        $numberAttack++;
        if( $playerHealth >= 1 && $monsterHealth <= 0 ) { 
          return TRUE;
        } elseif ( $monsterHealth >= 1 && $playerHealth <=0 ) { 
          return FALSE;
        }
      }while( $playerHealth >= 0 && $monsterHealth >= 0 );
    }
//Pobieranie listy ekspedycji
  function checkExpeditions( $player_lvl ) {
      $expeditions = mysql_query("SELECT * FROM expedition");
      while($r = mysql_fetch_assoc($expeditions)) {
        $id = $r['id'];
        $name = $r['name'];
        $lvl_required = $r['level_required'];
        $monster_id = $r['monster_id'];
        if($lvl_requred <= $player_lvl)
        {
          echo $id.'.  <a href="?id=fight&location='.$id.'">'.$name.'</a><br />';
        } 
      }
    }
//Ustalenie lokalizacji wyprawy
  if(!$_GET['location']) {
      echo 'Lista lokacji!<br />';
      checkExpeditions(100);
  }
  else {
    $id = $_GET['location'];
    $select_location_name = mysql_query("SELECT name FROM expedition WHERE id ='$id' ");
    while($r = mysql_fetch_assoc($select_location_name)) {
      $name = $r['name'];
    }
    $location = $name;
    echo 'Czy napewno chcesz sie udac do ' .$location. '?<br />';
    echo '<a href="?id=fight&location=' .$id. '&confirm=1">Oczywiście, idę do ' .$location. '</a> | <a href="?id=fight&location=' .$id. '&confirm=0">Rezygnuje!</a> <br />';
    if( $_GET['confirm']== '1' ) {
      echo '<br />Zaatakowałes ' .$location. ' ! <br />';
      $select_monster = mysql_query("SELECT monster_id FROM expedition WHERE name='$location'");
      while($r = mysql_fetch_assoc($select_monster)) {
        $monster = $r['monster_id'];
        $monster = explode( ';' , $monster );
        $count_monster = count($monster);
      }
      $rand = rand(0,1);
      $monster = $monster[$rand];
      $select_monster_stats = mysql_query("SELECT name, hp, attack, defense, exprience FROM monsters WHERE id='$monster'");
      while($a = mysql_fetch_assoc($select_monster_stats)) {
        $monster_name = $a['name'];
        $monster_hp = $a['hp'];
        $monster_attack = $a['attack'];
        $monster_defense = $a['defense'];
        $monster_experience = $a['experience'];
      }
      if( monsterFight( $monster_hp, 100, $monster_attack, 10, $monster_defense, 10 ) === TRUE) {
        echo '<div class="win">Gratulację! Zabiłeś ' .$monster_name. ' i otrzymałeś:</div>';
      } else{
        echo $monster_name.' WIN';
      }
    }
    elseif($_GET['confirm']== '0'){
      echo 'Rezygnujesz!';
    }
  }
?>
